package org.dromara.swimming.controller;

import  java.util.List;
import java.util.concurrent.ScheduledExecutorService;


import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mail.utils.MailUtils;
import org.dromara.common.websocket.utils.WebSocketUtils;
import org.dromara.swimming.service.IEmailService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.swimming.domain.vo.SwimmingVo;
import org.dromara.swimming.domain.bo.SwimmingBo;
import org.dromara.swimming.service.ISwimmingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * 景点侦测
 *
 * @author Ashufen
 * @date 2024-05-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/swimming/image")
public class SwimmingController extends BaseController {
    private final ScheduledExecutorService scheduledExecutorService;
    private final ISwimmingService swimmingService;
    private final IEmailService emailService;
    /**
     * 查询景点侦测列表
     */
    @SaCheckPermission("swimming:image:list")
    @GetMapping("/list")
    public TableDataInfo<SwimmingVo> list(SwimmingBo bo, PageQuery pageQuery) {
        return swimmingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出景点侦测列表
     */
    @SaCheckPermission("swimming:image:export")
    @Log(title = "景点侦测", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SwimmingBo bo, HttpServletResponse response) {
        List<SwimmingVo> list = swimmingService.queryList(bo);
        ExcelUtil.exportExcel(list, "景点侦测", SwimmingVo.class, response);
    }

    /**
     * 获取景点侦测详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("swimming:image:query")
    @GetMapping("/{id}")
    public R<SwimmingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(swimmingService.queryById(id));
    }

    /**
     * 新增景点侦测
     */
    @SaCheckPermission("swimming:image:add")
    @Log(title = "景点侦测", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SwimmingBo bo) {
        CompletableFuture.supplyAsync(() -> {
            // 执行异步任务
            Integer sum = bo.getNoSwimming() + bo.getSwimming() + bo.getReadySwimming();
            if (sum > 0) {
                String tos = emailService.listString();
                String message = "当前位置: " + bo.getLatitude() + ", " + bo.getLongitude() + "。准备游泳" + bo.getReadySwimming() + "人, 游泳" + bo.getSwimming() + "人, 不游泳" + bo.getNoSwimming() + "人。";
                Long userId = 1L;
                MailUtils.sendText(tos, "预警", message);
                scheduledExecutorService.schedule(() -> {
                    WebSocketUtils.sendMessage(userId, message);
                }, 3, TimeUnit.SECONDS);
            }
            return null;
        }).thenAccept(result -> {
            // 处理任务结果
            System.out.println(result);
        }).exceptionally(error -> {
            // 处理错误
            System.err.println(error.getMessage());
            return null;
        }).join();
        return toAjax(swimmingService.insertByBo(bo));
    }
    /**
     * 修改景点侦测
     */
    @SaCheckPermission("swimming:image:edit")
    @Log(title = "景点侦测", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SwimmingBo bo) {
        return toAjax(swimmingService.updateByBo(bo));
    }

    /**
     * 删除景点侦测
     *
     * @param ids 主键串
     */
    @SaCheckPermission("swimming:image:remove")
    @Log(title = "景点侦测", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(swimmingService.deleteWithValidByIds(List.of(ids), true));
    }
}
